package com.inews.business.wangpan.baidu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.inews.base.mapper.JsonMapper;
import com.inews.business.vo.BaiduResultVo;
import com.inews.business.vo.JinShanResultVo;
import com.inews.business.wangpan.jinshan.KuaiPanUtil;

public class BaiduPan {

	public static void main(String args[]) throws Exception{
		
		deleteFile("/apps/测试应用/j.txt");
//		
//		Date d=new Date();
//		d.setTime(1234567890);
//		System.out.println(DateFormatUtils.format(d, "yyyy-MM-dd hh:mm:ss"));
//		String result=info();
//		BaiduResultVo extdata=JsonMapper.nonEmptyMapper().JSONtoObject(result, BaiduResultVo.class);
//System.out.println(extdata.getList().size());
//    Date a=new Date();
   // a.setTime(time)
		//		download();
//		System.out.println(URLEncoder.encode("/apps/测试应用/j.txt", "UTF-8"));
		// HttpClient a = new DefaultHttpClient();
		// HttpGet request = new
		// HttpGet("https://pcs.baidu.com/rest/2.0/pcs/file?method=list&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414&path=%2Fapps%2F%E6%B5%8B%E8%AF%95%E5%BA%94%E7%94%A8%2F&by=time&order=asc");
		// // List list = new ArrayList();
		// // list.add(new BasicNameValuePair("config", "RGC"));
		// // list.add(new BasicNameValuePair("resType", "json"));
		// // list.add(new BasicNameValuePair("coors", abc.getGpsLongitude() +
		// "," + abc.getGpsLatitude()));
		// // list.add(new BasicNameValuePair("flag", "true"));
		// // list.add(new BasicNameValuePair("a_k",
		// "4fb0161a8d91862ebea3d070db80ae583893befa8c4aaee1becead80a9428d702830770ecb1301f3"));
		// //UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
		// "UTF-8");
		// //request.setURI();
		// HttpResponse response = a.execute(request);
		// System.out.println(EntityUtils.toString(response.getEntity()));
		String url = "https://pcs.baidu.com/rest/2.0/pcs/file?method=upload&path=%2Fapps%2F%E6%B5%8B%E8%AF%95%E5%BA%94%E7%94%A8%2Fj.txt&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414";

		//BaiduPan.UploadFile(url, new File("D://a.txt"));

	}

	
	public static String info() throws Exception, IOException{
		
		
		HttpClient a = new DefaultHttpClient();
		 HttpGet request = new	 HttpGet("https://pcs.baidu.com/rest/2.0/pcs/file?method=list&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414&path=%2Fapps%2F%E6%B5%8B%E8%AF%95%E5%BA%94%E7%94%A8%2F&by=time&order=asc");
		 HttpResponse response = a.execute(request);
		 String result=EntityUtils.toString(response.getEntity());
			System.out.println(result);
			return result;
	}
	
	
	public static String deleteFile(String fileName) throws Exception{
		DefaultHttpClient hc = new DefaultHttpClient();
		hc.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE);
		hc.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8"); // 默认为ISO-8859-1
		hc.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET,"UTF-8"); // 默认为US-ASCII		
		HttpPost httpRequest = new HttpPost("https://pcs.baidu.com/rest/2.0/pcs/file?method=delete&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414&path="+URLEncoder.encode(fileName, "UTF-8"));
		//httpRequest.setEntity(new StringEntity("method=delete&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414&path="+URLEncoder.encode(fileName, "UTF-8"), HTTP.UTF_8));
		HttpResponse response = hc.execute(httpRequest);
		String result=EntityUtils.toString(response.getEntity());
		System.out.println(result);
		return  result;
	}
	
	
	public static void UploadFile(File file,String fileName) throws Exception {

		org.apache.commons.httpclient.HttpClient hc = new org.apache.commons.httpclient.HttpClient();
		String path=URLEncoder.encode("/apps/测试应用/"+fileName, "UTF-8");
		String url = "https://pcs.baidu.com/rest/2.0/pcs/file?method=upload&path="+path+"&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414";

		PostMethod post = new PostMethod(url);
		String responseString = null;
		try {

			FilePart filePart = new FilePart("file", file);
			filePart.setTransferEncoding("binary");
			InputStreamEntity a;

			Part[] parts = new Part[] { filePart };

			post.setRequestEntity(new MultipartRequestEntity(parts, post
					.getParams()));
			int responseCode = hc.executeMethod(post);
			if (responseCode == 200) {
				responseString = post.getResponseBodyAsString();
			} else {
				throw new Exception(post.getStatusText() + "response code is "
						+ responseCode);
			}
		} catch (FileNotFoundException fe) {
			throw new Exception(" File does not exist!" + fe.toString());
		} catch (HttpException he) {
			throw new Exception("Problems with http protocol" + he.toString());
		} catch (IOException ioe) {
			throw new Exception("Problems while uploading the file"
					+ ioe.toString());
		} finally {
			post.releaseConnection();
		}
	}

	
	/*
     * 下载文件
     */
    private static InputStream DownFile(String url) throws Exception {
    	DefaultHttpClient hc = new DefaultHttpClient();

		hc.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.TRUE);
		hc.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8"); // 默认为ISO-8859-1
		hc.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET,"UTF-8"); // 默认为US-ASCII

		HttpGet hGet = new HttpGet(url);
		
		HttpResponse response = hc.execute(hGet);
		InputStream is = response.getEntity().getContent();
		
		
		 //垃圾回收
        System.gc();
        return is;
    }
	
	public static InputStream download(String fileName) throws Exception {
		String path=URLEncoder.encode(fileName, "UTF-8");
		InputStream is=DownFile("https://pcs.baidu.com/rest/2.0/pcs/file?method=download&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414&path="+path);
		return is;
		
		
//		HttpClient a = new DefaultHttpClient();
//		HttpGet request = new HttpGet(
//				"https://pcs.baidu.com/rest/2.0/pcs/file?method=download&access_token=3.7c771547fd1052ba7e260d678632928c.2592000.1371611606.3355784565-248414&path=%2Fapps%2F%E6%B5%8B%E8%AF%95%E5%BA%94%E7%94%A8%2Fa.txt");
//		HttpResponse response = a.execute(request);
//		System.out.println(EntityUtils.toString(response.getEntity()));
	}
}
